package com.rytx.web.controller.ysp;

import com.rytx.common.annotation.Log;
import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.core.domain.model.LoginUser;
import com.rytx.common.core.page.TableDataInfo;
import com.rytx.system.domain.ysp.ArtCartItem;
import com.rytx.system.service.ArtCartItemService;
import com.rytx.system.service.IArtRentalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/artCartItem")
@Api(value = "ArtCartItemController", tags = {"购物车商品项操作接口"})
public class ArtCartItemController extends BaseController {
    @Autowired
    private ArtCartItemService artCartItemService;

    @Autowired
    private IArtRentalService artRentalService;

    @DeleteMapping("/app/delete/{ids}")
    @ApiOperation(value = "根据ID数组删除购物车商品项", notes = "根据ID数组批量删除购物车商品项")
    public AjaxResult deleteByIds(@ApiParam(value = "购物车ID数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(artCartItemService.deleteByIds(Arrays.asList(ids)));
    }

    @PostMapping("/app/add")
    @ApiOperation(value = "添加购物车商品项", notes = "添加一个新的购物车商品项")
    public AjaxResult insertArtCartItem(@ApiParam(value = "商品项对象", required = true) @RequestBody ArtCartItem artCartItem)
    {
        artCartItem.setUserId(getLoginUser().getArtUser().getId());
        return toAjax(artCartItemService.insertArtCartItem(artCartItem));
    }

    @GetMapping("/app/artCartItemList")
    @ApiOperation(value = "分页查询购物车列表", notes = "分页查询购物车列表")
    public TableDataInfo artCartItemListByUserId()
    {
        LoginUser loginUser = getLoginUser();
        startPage();
        ArtCartItem artCartItem = new ArtCartItem();
        artCartItem.setUserId(loginUser.getArtUser().getId());
        List<ArtCartItem> list = artCartItemService.selectArtCartItemListByUserId(artCartItem);
        List<Map<String,Object>> listMap = new ArrayList<>();
        for (ArtCartItem item : list) {
            Map<String,Object> map = new HashMap<>();
            map.put("cart",item);
            map.put("rental",artRentalService.selectArtRentalById(item.getArtRentalId()));
            listMap.add(map);
        }
        return getDataTable(listMap);
    }

    @GetMapping
    public List<ArtCartItem> findAll() {
        return artCartItemService.findAll();
    }

    @GetMapping("/{id}")
    public ArtCartItem findById(@PathVariable Long id) {
        return artCartItemService.findById(id);
    }

    @PostMapping
    public int save(@RequestBody ArtCartItem artCartItem) {
        return artCartItemService.save(artCartItem);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id) {
        artCartItemService.deleteById(id);
    }
}

